/* ====================================================================
 *         Projeto: jversion-core
 *         Arquivo: RepositoryUtil.java
 * Data de Criação: Aug 30, 2011
 * -------------------------------------------------------------------
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.jversion.integration.scm.svn.util;

import java.io.File;

import org.apache.commons.lang.StringUtils;
import org.jversion.integration.scm.exceptions.RepositoryCreationFailureException;
import org.jversion.integration.scm.model.Repository;
import org.jversion.integration.scm.model.types.RepositoryType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;

/**
 * Classe utilitária com o objetivo de fornecer métodos de manipulação de repositórios SVN.
 * @since 1.0.0
 * @author Jorge Luiz Monteiro
 */
public abstract class RepositoryUtil {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryUtil.class);
	
	public static void createRepository(Repository repository) {
		createRepository(repository, false);
	}
	
	public static void createRepository(Repository repository, boolean overwrite) {
		
		if (repository == null) {
			throw new IllegalArgumentException("O repósitório não foi informado.");
		}
		
		if (repository.getType() != RepositoryType.FILE) {
			throw new IllegalArgumentException("Somente é possível criar repositórios locais.");
		}
		
		if (StringUtils.isEmpty(repository.getLocation())) {
			throw new IllegalArgumentException("A localização do repositório não foi informada ou é inválida");
		}
		
		LOGGER.info("Criando repositório {}.", repository.getLocation());
		try {
			
			File location = new File(repository.getLocation());
			if (location.exists() && !overwrite) {
				LOGGER.error("O Caminho {} já existe", repository.getLocation());
				throw new RepositoryCreationFailureException("O caminho informado já existe.");
			}
			
			SVNRepositoryFactory.createLocalRepository(
				location, true, overwrite);
			
			LOGGER.info("Repositório criado com sucesso em {}.", repository.getLocation());
			
		} catch (SVNException exception) {
			LOGGER.error(exception.getMessage(), exception);
			throw new RepositoryCreationFailureException(exception);
		}
		
	}

}
